<%@ include file="css/header.jsp" %>
<title>Manage users</title>
<%
String userId;
String manageUsers;
if((String)session.getAttribute("userId")==null||((String)session.getAttribute("userId")).trim().length() == 0) userId = "null";
else userId = (String)session.getAttribute("userId");
if((String)session.getAttribute("manageUsers")==null||((String)session.getAttribute("manageUsers")).trim().length() == 0) manageUsers = "null";
else manageUsers = (String)session.getAttribute("manageUsers");
	if(!(userId.equals("null"))){
		if(manageUsers.equals("1")){
%>

<script type="text/javascript">
	var jqueryOption = {};
	jqueryOption.grdRowNum = 50;
	jqueryOption.grdRowList = [50, 100, 200];
	jqueryOption.grdJsonReader = {
	    root: "rows",
	    page: "page",
	    total: "total",
	    records: "records",
	    repeatitems: true, //if 'false' will chage json reader format to 'name' require
	    //cell: "cell",
	    id: "0" //id: "id"
	};


$(document).ready(function() {
	$('#mnu li').hover(
	        function () {
	            //show its submenu
	            $('ul', this).slideDown(100);
	 
	        }, 
	        function () {
	            //hide its submenu
	            $('ul', this).slideUp(100);         
	        }
	);
    
	$("#btnAdd").click(function(){
		window.location.href = "AddAdminServlet";
	    return false;
	});
       
    $("#grdAdmin").jqGrid({
        	url:'<%=request.getContextPath()%>/ManageAdminServlet/DisplayAdmin',
        	datatype: 'json',
            mtype: 'POST',
            colNames:['id','Username','Group','Last login','Edit','Delete',],
            colModel:[
                {name:'id', index:'id',title:false, width:10, sortable:true,hidden:true},
                {name:'username',index:'username', width:27,sortable:false},
                {name:'group',index:'group', width:27,sortable:false},
                {name:'lastlogin',index:'lastlogin',align:'center', width:20,sortable:false},
                {name:'editAdmin', align:'center',index:'editAdmin', width:8,sortable:false},
                {name:'deleteAdmin',align:'center', index:'deleteAdmin', width:8,sortable:false}
            ],
            postData: {
            },
            rowNum: jqueryOption.grdRowNum, //50
            rowList: jqueryOption.grdRowList, //[50, 100, 200]
            autowidth: true, //Equal Container !Not change when resize window
            height: -1, //Auto height
            scrollOffset: 1, //Hide scroll bar
            pager: '#pagerAdmin',
            rownumbers: true,
            viewrecords: true,
            sortname: 'id',
            sortorder: "desc",
            cache: false,
            caption: 'User',
            emptyrecords: 'No result',
            jsonReader : jqueryOption.grdJsonReader,
            onPaging: function(pgButton){ //pgButton : check which button click
            	bindGrdAdmin($("#grdAdmin").getGridParam('page'));
            },
            onSortCol: function(index, iCol, sortorder){
            	bindGrdAdmin(1)
            },
            loadError: function(xhr, status, error) {
            	//TrackViewer.doError();
            },
            loadComplete: function(data) {
            	 try {
 					if(data.error != null){
 	                	$.msgbox(data.error, {
 	                        type : 'error'
 	                    }, function(buttonPressed) {
 	                    
 	                    });
 					}
 				} catch (e) {
 					
 				}
            },gridComplete: function() {
                var gridObj = $("#grdAdmin");
                var ids = gridObj.jqGrid('getDataIDs');
                var contentValue;
                for (var i = 0; i < ids.length; i++) {
                    var rowid = ids[i];
                    var deleteAdmin = '<a href="javascript:void(0)" onClick="doDelete(\''+rowid+ '\'); return false">Delete</a>';
                    var editAdmin = '<a href="javascript:void(0)" onclick="doEdit(\''+rowid+ '\'); return false")>Edit</a>';
                    gridObj.jqGrid('setRowData',rowid,{editAdmin:editAdmin});
                    gridObj.jqGrid('setRowData',rowid,{deleteAdmin:deleteAdmin});
                }
            }
        });
    });

function bindGrdAdmin(ipage){
   $("#grdAdmin").jqGrid('setGridParam'
    ,{url:"<%=request.getContextPath()%>/ManageAdminServlet/DisplayAdmin"
        ,page: ipage}).trigger("reloadGrid");
}



function doDelete(rowdata){
	$.msgbox('Confirm Delete or not ?', {
        type : 'confirm',
        buttons : [
            {type: 'submit', value:'Yes'},
            {type: 'cancel', value:'No'}
        ]
    }, function(buttonPressed) {
        if(buttonPressed == 'Yes'){
            $.ajax({
                type: 'POST',
                url: '<%=request.getContextPath()%>/ManageAdminServlet/DeleteAdmin?rowdata='+rowdata,
                dataType: 'json',
                success: function(data){
                    $.each(data, function(key, value) {
                        //Validate is error send?
                        if (key == 'error') {
                            //alert(value);
                            $.msgbox(value , {
                                type : 'error'
                            }, function(buttonPressed) {
                                // your code here
                            });
                        } else { //No Error
                        	 bindGrdAdmin(1);
                        }
                        return false; //break each loop
                    });
                },
                error: function(){
                    $.msgbox('Error', {
                        type : 'error'
                    }, function(buttonPressed) {
                        // your code here
                    });
                }
            });
        }
    });
}

function doEdit(id){
	window.location.href = "EditAdminServlet?id="+id;
}
</script>
<body>
<div id="jp-main">
        <div class="jp-sheet">
            <div class="jp-sheet-tl"></div>
            <div class="jp-sheet-tr"></div>
            <div class="jp-sheet-bl"></div>
            <div class="jp-sheet-br"></div>
            <div class="jp-sheet-tc"></div>
            <div class="jp-sheet-bc"></div>
            <div class="jp-sheet-cl"></div>
            <div class="jp-sheet-cr"></div>
            <div class="jp-sheet-cc"></div>
            <div class="jp-sheet-body">
                <div class="jp-header">
                    <div class="jp-header-center">
                        <div class="jp-header-png"></div>
                        <div class="jp-header-jpeg"></div>
                    </div>
                        <div class="jp-header-fluid-object"></div>
                </div>
                <div class="jp-nav">
                	<div class="l"></div>
                	<div class="r"></div>
                	<ul class="jp-menu">
                		
                			<%if (session.getAttribute("tcdTracking").equals("1")){%>
					    	  <li>
					    	  	<a href="ViewServlet"><span class="l"></span><span class="r"></span><span class="t">TCD Tracking</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("rdTracking").equals("1")){%>
					    	  <li>
					    	  	<a href="ViewServletRD"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">RD Tracking</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("tcdBilling").equals("1")){%>
					    	  <li>
					    	  	<a href="TCDBillingServlet"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">TCD Billing</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("rdBilling").equals("1")){%>
					    	  <li>
					    	  	<a href="RDBilling"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">RD Billing</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (!(userId.equals("0"))){%>
					    	  <li>
					    	  	<a href="EditProfile.jsp"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">Edit profile</span></a>                		
                			  </li>
					      	<%} %>
                			
           		  		<li>
                			<a href="#" class="active"><span class="l"></span><span class="r"></span><span class="t">User Management</span></a>
							<ul>
								<%if (session.getAttribute("manageUsers").equals("1")){%>
						    		<li><a href="ManageAdminServlet">Manage users</a></li>
						      	<%} %>
						      	<%if (session.getAttribute("manageGroups").equals("1")){%>
					    			<li><a href="ManageGroupServlet">Manage groups</a></li>
					      		<%} %>
					      		<%if (session.getAttribute("userLog").equals("1")){%>
					    			<li><a href="UserLogServlet">User log</a></li>
					      		<%} %>
           					</ul>
               		  	</li>		
           		  	  	<li>
                			<a href="Logout"><span class="l"></span><span class="r"></span><span class="t">Logout</span></a>                		
                	  	</li>
               	  </ul>
                </div>
                <div class="jp-content-layout">
                  <div class="jp-post-body">
                     <h2 class="jp-postheader">Manage users</h2>
                      <div class="jp-postcontent">
                      
                       <fieldset id="fsManageAdmin">
			            <legend class="pageTitleForm"><h3>Manage users</h3></legend>
			            <div>&nbsp;<div>
			            <div id="content-placeholder">
			            	<div id="jqgrid">
			            		<span>
			                        <button id="btnAdd" type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" ><span class="ui-button-icon-primary ui-icon ui-icon-plus"></span><span class="ui-button-text">Add</span></button>
			                    </span>
			                    <table id="grdAdmin"></table>
			                    <div id="pagerAdmin"></div>
			                 </div>
			             </div>
			        </fieldset>                  
			        	
                      </div>
                  </div>
                </div>
            </div>
        </div>
</div>
    
</body>	
<%
		}else{
%>
<script type="text/javascript">
    	window.location.href = "EditProfile";
</script>
<%
		}
	}else{
%>
<script type="text/javascript">
	window.location.href = "LoginServlet";
</script>
<%
	}
	
%>
</html>